﻿using System.Linq.Expressions;

namespace SimpleORM
{
    public static class MethodCallExpressionExtension
    {
        #region Methods

        public static LambdaExpression FindLambda(this MethodCallExpression source)
        {
            foreach (var item in source.Arguments)
            {
                UnaryExpression ue = item as UnaryExpression;
                if (ue == null)
                    continue;

                if (ue.Operand.NodeType == ExpressionType.Lambda)
                    return (LambdaExpression)ue.Operand;
            }

            return null;
        }

        public static MethodCallExpression FindFirst(this MethodCallExpression source)
        {
            if (source == null)
                return null;

            if (source.Arguments.Count == 0)
                return source;

            MethodCallExpression m = source.Arguments[0] as MethodCallExpression;
            if (m == null)
                return source;

            return m.FindFirst();
        }

        #endregion
    }
}
